package com.xoa.controller.mealmanage;

import com.xoa.model.mealmanage.MealManage;
import com.xoa.service.mealmanage.MealManageService;
import com.xoa.util.DateFormat;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/MealManage")
public class MealManageController {

    @Resource
    private MealManageService mealManageService;

    //用餐预定
    @RequestMapping("/mealReservation")
    public String mealReservation() {
        return "app/rpm/haveMeals/mealReservation";
    }

    //用餐汇总
    @RequestMapping("/summary")
    public String summary() {
        return "app/rpm/haveMeals/summary";
    }

    //用餐审批页面
    @RequestMapping("/mealVerify")
    public String mealVerify(){
        return "/app/rpm/haveMeals/mealVerify";
    }

    /**
     * 作者 廉立深
     * 日期 2020/10/23
     * 方法介绍  查询用餐管理
     * 参数 [request, page, limit, mealManage]
     * 返回 com.xoa.util.ToJson
     **/
    @RequestMapping("/getMealMap")
    @ResponseBody
    public ToJson getMealMap(HttpServletRequest request, HttpServletResponse response, Integer page, Integer limit,boolean useFlag, MealManage mealManage){
        Map map = new HashMap();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(useFlag);
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        ToJson mealMap = mealManageService.getMealMap(request, map, mealManage);

        try {
            //导出
            if (!StringUtils.checkNull(request.getParameter("isExport")) && mealMap.isFlag()){
                List<MealManage> manageList = mealMap.getObj();
                manageList.forEach(item->{
                    //用餐类型
                    item.setMealType(item.getMealType().equals("1") ? "访客" : "教职工");
                    //审核状态
                    switch (item.getCollege()){
                        case "0":
                            item.setCollege("待审批");
                            break;
                        case "1":
                            item.setCollege("通过");
                            break;
                        case "2":
                            item.setCollege("未通过");
                            break;
                    }
                    //时间
                    item.setPredestineTimestr(DateFormat.getDatestrTime(item.getPredestineTime()));
                });

                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("申报任务管理", 4);
                String[] secondTitles = { "申请人", "部门", "用餐类型", "审核状态", "早餐（份）", "中餐（份）", "预定时间" };
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = { "userName", "deptName", "mealType", "college", "breakfastNum", "lunchNum", "predestineTimestr" };

                HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, manageList, beanProperty);
                ServletOutputStream out = response.getOutputStream();

                String filename = "用餐汇总.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mealMap;
    }


    /**
     * 作者 廉立深
     * 日期 2020/10/23
     * 方法介绍  编辑接口
     * 参数 [request, mealManage]
     * 返回 com.xoa.util.ToJson
     **/
    @RequestMapping("/editMealMap")
    @ResponseBody
    public ToJson editMealMap(HttpServletRequest request, MealManage mealManage){
        return mealManageService.editMealMap(request,mealManage);
    }

    /**
     * 作者 廉立深
     * 日期 2020/10/23
     * 方法介绍  删除
     * 参数 [request, mealManage]
     * 返回 com.xoa.util.ToJson
     **/
    @RequestMapping("/delMealId")
    @ResponseBody
    public ToJson delMealId(HttpServletRequest request, MealManage mealManage){
        return mealManageService.delMealId(request,mealManage);
    }




}
